<?php

namespace Yxchat\Model;

use Think\Model;
use Common\Model\ModelException;
use Ruben\Yxyun\Yxim;

class CallbackNoticeModel extends Model {

    protected $_auto = [
        ['create_time', 'time', self::MODEL_INSERT, 'function'],
        ['update_time', 'time', self::MODEL_BOTH, 'function'],
    ];
    protected $_validate = [];
    protected $_scope = [
        'valid' => [
            'where' => ['closed' => 0]
        ]
    ];

    //结伴添加好友，同时再添加关注
    public function addFriend($body) {
        $UserFriendsM = D('UserFriends');
        $Model = M();
        $Model->startTrans();
        //1,写入userFriend
        //2,互相关注
        if ($body['verifyType'] == 3 || $body['verifyType'] == 1) {
            $UserFriends = [
                'accid' => $body['fromAccount'],
                'faccid' => $body['toAccount'],
                'msg' => $body['msg'],
                'type' => $body['verifyType'],
                'add_time' => time(),
            ];
            $id = $UserFriendsM->add($UserFriends);
            $result1 = D('Account/UserFavorite')->addFavorites($body['fromAccount'], [$body['toAccount']]);
            $result2 = D('Account/UserFavorite')->addFavorites($body['toAccount'], [$body['fromAccount']]);
            if ($id && $result1 && $result2) {
                $imConfig = C('sdk.yxim');
                $yxim = new Yxim($imConfig);
                $Model->commit();
                // ob_end_clean();
                // ob_start();
                // echo 1;
                //ob_end_flush();
				// flush();
                // if (function_exists("fastcgi_finish_request")) { // yii或yaf默认不会立即输出，加上此句即可（前提是用的fpm）
                //     fastcgi_finish_request(); // 响应完成, 立即返回到前端,关闭连接
                // }
//                sleep(1);
                // ignore_user_abort(true); // 在关闭连接后，继续运行php脚本
                // set_time_limit(0);
//                $users = D('Users')->where(['user_id' => ['in', [$body['fromAccount'], $body['toAccount']]]])->select();
//                $reult[] = $yxim->updateFriend($users[0]['user_id'], $users[1]['user_id'], $users[1]['nickname']);
//                $reult[] = $yxim->updateFriend($users[1]['user_id'], $users[0]['user_id'], $users[0]['nickname']);
//                \Think\Log::record(json_encode($reult), '----更新备注----', TRUE);
            }
        }

        return 0;
    }

    //结伴添加好友，同时再添加关注
    public function delFriend($body) {
        $Model = M();
        $Model->startTrans();
        //1,写入userFriend
        $where = "((accid={$body['fromAccount']} and faccid={$body['toAccount']}) or (faccid={$body['fromAccount']} and accid={$body['toAccount']})) and type=3";
        $id = D('UserFriends')->where($where)->setField(['delete_time' => time()]);

        $result1 = D('Account/UserFavorite')->delFans($body['fromAccount'], $body['toAccount']);
        $result2 = D('Account/UserFavorite')->delFans($body['toAccount'], $body['fromAccount']);

        if ($id && $result1 && $result2) {
            $Model->commit();
            return 1;
        }
        $Model->rollback();
        return 0;
    }

    protected function _after_find(&$record, $options) {
        
    }

    protected function _after_select(&$records, $options) {
        
    }

}
